0.0.1 Aim

  • Create reproducible report (HTML, PDF)
  • Application of R in ecology with example
  • Packages in R
  • Plotting in R

1 Rmarkdown

Lets discuss reproducible report.

2 Ecology applications

2.1 Student’s t-test

Check weather the observation we have are significantly differ from their mean.

2.1.1 One sample t-test

Let have weight of birds from yesterdays survey. Suppose we have observation of weight always less than 70. Test the hypothesis now with this new data.

wts <-c (50, 55, 60, 63, 65, 66, 72, 75, 80)
wts
## [1] 50 55 60 63 65 66 72 75 80
t.test(wts)
## 
##  One Sample t-test
## 
## data:  wts
## t = 20.464, df = 8, p-value = 3.402e-08
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  57.77401 72.44821
## sample estimates:
## mean of x 
##  65.11111
?t.test
t.test(wts, alternative = "less", mu = 70, conf.level = 0.99)
## 
##  One Sample t-test
## 
## data:  wts
## t = -1.5365, df = 8, p-value = 0.08148
## alternative hypothesis: true mean is less than 70
## 99 percent confidence interval:
##      -Inf 74.32689
## sample estimates:
## mean of x 
##  65.11111

2.1.2 Two sample t-test

wts <- c(50, 55, 60, 63, 65, 66, 72, 75)
wts_2 <- c(57, 63, 66, 72, 73, 80, 92, 95)
# independent 2-group t-test
ttest = t.test(wts,wts_2)
names(ttest)
##  [1] "statistic"   "parameter"   "p.value"     "conf.int"    "estimate"   
##  [6] "null.value"  "stderr"      "alternative" "method"      "data.name"

2.1.3 Paired t-test

# paired t-test
t.test(wts,wts_2,paired=TRUE) 
## 
##  Paired t-test
## 
## data:  wts and wts_2
## t = -5.65, df = 7, p-value = 0.0007745
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -16.312959  -6.687041
## sample estimates:
## mean of the differences 
##                   -11.5

2.2 ANOVA

Create a dataframe

df <- data.frame(cbind(wts, wts_2))
summary(df)
##       wts            wts_2      
##  Min.   :50.00   Min.   :57.00  
##  1st Qu.:58.75   1st Qu.:65.25  
##  Median :64.00   Median :72.50  
##  Mean   :63.25   Mean   :74.75  
##  3rd Qu.:67.50   3rd Qu.:83.00  
##  Max.   :75.00   Max.   :95.00
wt_stack <- stack(df)
aov_res <- aov(formula = values ~ ind, data = wt_stack)
aov_res
## Call:
##    aov(formula = values ~ ind, data = wt_stack)
## 
## Terms:
##                  ind Residuals
## Sum of Squares   529      1755
## Deg. of Freedom    1        14
## 
## Residual standard error: 11.1963
## Estimated effects may be unbalanced

Summary

summary(aov_res)
##             Df Sum Sq Mean Sq F value Pr(>F)  
## ind          1    529   529.0    4.22 0.0591 .
## Residuals   14   1755   125.4                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

2.3 Correlation

cor(mtcars)
##             mpg        cyl       disp         hp        drat         wt
## mpg   1.0000000 -0.8521620 -0.8475514 -0.7761684  0.68117191 -0.8676594
## cyl  -0.8521620  1.0000000  0.9020329  0.8324475 -0.69993811  0.7824958
## disp -0.8475514  0.9020329  1.0000000  0.7909486 -0.71021393  0.8879799
## hp   -0.7761684  0.8324475  0.7909486  1.0000000 -0.44875912  0.6587479
## drat  0.6811719 -0.6999381 -0.7102139 -0.4487591  1.00000000 -0.7124406
## wt   -0.8676594  0.7824958  0.8879799  0.6587479 -0.71244065  1.0000000
## qsec  0.4186840 -0.5912421 -0.4336979 -0.7082234  0.09120476 -0.1747159
## vs    0.6640389 -0.8108118 -0.7104159 -0.7230967  0.44027846 -0.5549157
## am    0.5998324 -0.5226070 -0.5912270 -0.2432043  0.71271113 -0.6924953
## gear  0.4802848 -0.4926866 -0.5555692 -0.1257043  0.69961013 -0.5832870
## carb -0.5509251  0.5269883  0.3949769  0.7498125 -0.09078980  0.4276059
##             qsec         vs          am       gear        carb
## mpg   0.41868403  0.6640389  0.59983243  0.4802848 -0.55092507
## cyl  -0.59124207 -0.8108118 -0.52260705 -0.4926866  0.52698829
## disp -0.43369788 -0.7104159 -0.59122704 -0.5555692  0.39497686
## hp   -0.70822339 -0.7230967 -0.24320426 -0.1257043  0.74981247
## drat  0.09120476  0.4402785  0.71271113  0.6996101 -0.09078980
## wt   -0.17471588 -0.5549157 -0.69249526 -0.5832870  0.42760594
## qsec  1.00000000  0.7445354 -0.22986086 -0.2126822 -0.65624923
## vs    0.74453544  1.0000000  0.16834512  0.2060233 -0.56960714
## am   -0.22986086  0.1683451  1.00000000  0.7940588  0.05753435
## gear -0.21268223  0.2060233  0.79405876  1.0000000  0.27407284
## carb -0.65624923 -0.5696071  0.05753435  0.2740728  1.00000000

3 Packages

Packages are collection of several functions in R.

To use a particular function from a package, it need to be downloaded and loaded.

3.0.1 Introduction to CRAN

This session introduces the essential of package discovery, installation, and use.

The web site at https://cran.r-project.org/ contains descriptions of all packages, as well as essential reference materials.

3.0.2 Install a package from CRAN

# download and install 'ggplot2' package from CRAN
install.packages("ggplot2")

A total of 2,400 packages comprising over 3,000 functions have been used in ecology and evolution, and the journal Methods in Ecology and Evolution for instance had the highest reported proportionate usage of all journals recently examined in a recent study (Lai et al., 2019). - A checklist for choosing between R packages in ecology and evolution

The number of R packages for common statistical and ecological/evolutionary concepts.

4 Plotting

4.1 Graphics

One of the main reasons data analysts turn to R is for its strong graphic capabilities.

4.2 Creating a Graph

attach(mtcars)
## The following object is masked from package:ggplot2:
## 
##     mpg
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")

Few plotting functions comes built-in when R installed. These can help do diffrent type of plots. Such as * Scater Plots * Dot Plots * Bar Plots * Pie Charts * Box Plots

4.3 Scatter Plots

plot(wt, mpg, main="Scatterplot Example",
   xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)

4.3.1 Dot Plots

dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7,
   main="Gas Milage for Car Models",
   xlab="Miles Per Gallon")

4.3.2 Bar Plots

4.3.2.1 Simple Bar Plot

counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution",
   xlab="Number of Gears")

4.3.2.2 Stacked Bar Plot

counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",
  xlab="Number of Gears", col=c("darkblue","red"),
  legend = rownames(counts))

4.3.2.3 Grouped Bar Plot

counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",
  xlab="Number of Gears", col=c("darkblue","red"),
  legend = rownames(counts), beside=TRUE)

4.3.3 Pie Charts

slices <- c(10, 12,4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie(slices, labels = lbls, main="Pie Chart of Countries")

4.3.4 Box Plots

4.3.4.1 Simple Boxplot

boxplot(mpg~cyl,data=mtcars, main="Car Milage Data",
   xlab="Number of Cylinders", ylab="Miles Per Gallon")

4.3.4.2 Notched Boxplot

boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  col=(c("gold","darkgreen")),
  main="Tooth Growth", xlab="Suppliment and Dose")
## Warning in bxp(list(stats = structure(c(8.2, 9.7, 12.25, 16.5, 21.5, 4.2, : some
## notches went outside hinges ('box'): maybe set notch=FALSE

4.4 Customization

Graphical parameters describes how to change a graph’s symbols, fonts, colors, and lines. Axes and text describe how to customize a graph’s axes, add reference lines, text annotations and a legend. Combining plots describes how to organize multiple plots into a single graph.

# Legend Example
attach(mtcars)
## The following objects are masked from mtcars (pos = 3):
## 
##     am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
## The following object is masked from package:ggplot2:
## 
##     mpg
boxplot(mpg~cyl, main="Milage by Car Weight",
   yaxt="n", xlab="Milage", horizontal=TRUE,
   col=terrain.colors(3))
legend("topright", inset=.05, title="Number of Cylinders",
   c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)

 

Created and Maintained by Sangram Keshari Sahu
Rmarkdown Template used from Rmdplates package
Licensed under CC-BY 4.0
Source Code At GitHub